package app

import (
	"bufio"
	"os"
	"strings"
	"time"

	"gitee.com/StarCitizen_CN/logger"
)

// 暂时先粗暴检查问题
// todo: 黑科技应用测试
func CrashWorkaround(err error, gamelogPath string) (text string, url string, isOpenLogFileRequired bool) {

	if err.Error() == "exit status 1" {
		Notification("游戏闪退啦", "啊这。。。")
		logger.Err(err)
		time.Sleep(time.Second * 1)
		os.Exit(0)
	}

	var lastLineOfErr string

	defMsg := "请截图！点击【是】将在游戏目录内找到 Game.log，并进入深空救援群。"
	f, openErr := os.Open(gamelogPath)
	if openErr == nil {
		defer f.Close()

		bs := bufio.NewScanner(f)
		bs.Split(bufio.ScanLines)

		for bs.Scan() {
			line := bs.Text()
			if workaroundText, url, isOpenLogFileRequired := workaroundByLine(line); workaroundText != "" {
				if workaroundText == "" || url == "" {
					continue
				}
				return workaroundText, url, isOpenLogFileRequired
			}

			lastLineOfErr = line
		}
	} else {
		lastLineOfErr = err.Error()
	}

	msgOfLastLine, linkOfLastLine, isOpenLogFileRequired := workaroundByLastLine(lastLineOfErr)
	if msgOfLastLine != "" && linkOfLastLine != "" {
		return msgOfLastLine, linkOfLastLine, isOpenLogFileRequired
	}

	return defMsg, DSR_QG_LINK, true
}

func workaroundByLastLine(text string) (suggest string, jumper string, isOpenLogFileRequired bool) {
	return workaroundByLine(text)
}

func workaroundByLine(text string) (suggest string, jumper string, isOpenLogFileRequired bool) {

	if strings.Contains(text, "STATUS_CRYENGINE_OUT_OF_SYSMEM") {
		s, j := resolveOOM()
		return s, j, false
	}

	if strings.Contains(text, "EXCEPTION_ACCESS_VIOLATION") {
		s, j := resolveEAV()
		return s, j, false
	}

	if strings.Contains(text, "DXGI_ERROR_DEVICE_REMOVED") {
		s, j := resolveDDR()
		return s, j, true
	}

	if strings.Contains(text, "Wakeup socket sendto error 10049") {
		s, j := resolveXHH()
		return s, j, false
	}

	if strings.Contains(text, "network.replicatedEntityHandle") {
		s, j := resolveGameCrash(text)
		return s, j, true
	}

	if strings.Contains(text, "The requested operation requires elevated") {
		s, j := resolveElevationRequires()
		return s, j, false
	}

	if strings.Contains(text, "The process cannot access the file because is is being used by another process") {
		s, j := resolveElevationRequires()
		return s, j, false
	}

	return "", "", true
}

// ----------------------------- “建议”的逻辑 ↓ ----------------------------
func resolveOOM() (string, string) {
	return "请截图来告知其他人发生了什么。当前系统内存不足，请设置虚拟内存并清理其他中运行的程序。\n需要帮助请按【是】进入汉化群。", SCZ_QG_LINK
}

func resolveEAV() (string, string) {
	c0000005Link := "https://docs.qq.com/doc/DUURxUVhzTmZoY09Z"
	return "请截图来告知其他人发生了什么。\n游戏遇到了触发原因广泛的崩溃问题。请关闭杀软、翻译、游戏辅助等工具。\n持续在启动阶段崩溃请尝试移除一些外设（摇杆）。\n问题在重装系统之后仍存在的话，请进行内存测试。\n点击【是】跳转查查阅更多信息。", c0000005Link
}

func resolveDDR() (string, string) {
	return "请截图来告知其他人发生了什么。点击【是】将告知你更多关于 DXGI_ERROR_DEVICE_REMOVED 的信息。", DDR_Link
}

// *黑盒
func resolveXHH() (string, string) {
	return "请截图来告知其他人发生了什么。\n你可能开启了某黑盒加速器，请将其调整至模式二。\n需要帮助请按【是】进入汉化群。", SCZ_QG_LINK
}

// launcher
func resolveElevationRequires() (string, string) {
	return "游戏启动失败，因为游戏需要管理员权限才能启动。请退出启动器并右键RSI启动器使用管理员权限启动游戏。请截图，点【是】将进入中文社区群。", SCC_QG_LINK
}

// launcher
// 启动器占用进程的问题
func resolveExeAccess() (string, string) {
	return "游戏启动失败，因为游戏程序被启动器占用。请重启启动器。请截图，点【是】将进入中文社区群。", SCC_QG_LINK
}

func resolveGameCrash(point string) (string, string) {
	Notification("游戏故障闪退啦", "请按提示操作~")
	return "游戏故障闪退啦\n如需帮助请截图本窗口，告知救援中心发生了什么。\n" + point + "\n需要帮助按【是】进入救援中心", DSR_QG_LINK
}
